<?php


namespace App\Service\Admin;


use App\Constants\ContextKey;
use App\Dao\CooperativeDao;
use App\Model\AdminUser;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\Context;

class BaseService
{
    /**
     * @Inject()
     * @var CooperativeDao
     */
    protected $cooperativeDao;

    protected function user(): AdminUser
    {
        return Context::get(ContextKey::ADMIN_USER);
    }

    /**
     * 将 where 条件里面的 区域 转换成 合作商
     */
    public function formatWhereRegionToCooperative(array $where)
    {
        $whereRegion = [];
        if (isset($where['province'])) {
            if ($where['province']) {
                $whereRegion['province'] = $where['province'];
            }
            unset($where['province']);
        }
        if (isset($where['city'])) {
            if ($where['city']) {
                $whereRegion['city'] = $where['city'];
            }
            unset($where['city']);
        }

        if(!empty($whereRegion)){
            $cooperativeIds = $this->cooperativeDao->getCooperativeIdsByRegion($whereRegion);
            $where['cooperative_ids'] = $cooperativeIds;
        }
        return $where;
    }

    /**
     * 提取分页信息
     */
    public function extractPaginateInfo(array &$where)
    {
        $pageInfo = [];
        if (isset($where['page'])) {
            $pageInfo['page'] = $where['page'];
            unset($where['page']);
        }
        if (isset($where['perPage'])) {
            $pageInfo['perPage'] = $where['perPage'];
            unset($where['perPage']);
        }
        if (isset($where['total'])) {
            $pageInfo['total'] = $where['total'];
            unset($where['total']);
        }

        return $pageInfo;
    }
}